package cn.thornbird.orgsync.helper.converter;

import cn.thornbird.orgsync.helper.AppHelper;
import cn.thornbird.orgsync.model.AppDepartmentData;
import cn.thornbird.orgsync.model.AppUserData;
import cn.thornbird.orgsync.model.wechat.DepartmentList;
import cn.thornbird.orgsync.model.wechat.UserInfo;
import cn.thornbird.orgsync.model.wechat.UserList;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;

public class WechatConverter extends DataConverter<DepartmentList.Department, UserList.User, UserInfo> {

    @Override
    public AppDepartmentData parseDepartment(DepartmentList.Department department) {
        AppDepartmentData departmentData = new AppDepartmentData();
        departmentData.setId(department.getId().toString());
        departmentData.setParentId(department.getParentId() == null ? null : department.getParentId().toString());
        departmentData.setName(department.getName());
        departmentData.setEnglishName(department.getEnglishName());
        departmentData.setOrder(department.getOrder());
        return departmentData;
    }

    @Override
    public AppUserData parseUser(UserList.User user) {
        if (user == null) {
            return null;
        }

        AppUserData userData = new AppUserData();
        userData.setId(user.getUserId());
        userData.setName(user.getName());
        userData.setAvatar(user.getThumbAvatar());
        if (!StringUtils.isEmpty(user.getGender())) {
            userData.setGender(Integer.parseInt(user.getGender()));
        }
        userData.setMobile(user.getMobile());
        userData.setEmail(user.getEmail());
        userData.setTelephone(user.getTelephone());
        userData.setTitle(user.getPosition());
        if (user.getDirectLeader() != null && !user.getDirectLeader().isEmpty()) {
            userData.setLeaderId(user.getDirectLeader().get(0));
        }
        if (user.getMainDepartment() != null) {
            userData.setMainDepartmentId(user.getMainDepartment().toString());
        }

        if (user.getDepartment() != null) {
            userData.setDepartments(new HashMap<>());
            for (int i = 0, count = user.getDepartment().size(); i < count; i++) {
                String id = user.getDepartment().get(i).toString();
                Long order = user.getOrder().get(i);
                Integer leader = user.getIsLeaderInDepartment().get(i);
                AppUserData.DepartmentInfo departmentInfo = new AppUserData.DepartmentInfo();
                departmentInfo.setOrder(order);
                departmentInfo.setLeader(leader == 1);
                userData.getDepartments().put(id, departmentInfo);
            }
        }

        return userData;
    }

    @Override
    public UserInfo toUserInfo(AppUserData userData) {
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(userData.getId());
        userInfo.setName(userData.getName());
        if (userData.getGender() != null) {
            userInfo.setGender(userData.getGender().toString());
        }
        userInfo.setMobile(AppHelper.getPureMobile(userData.getMobile()));
        userInfo.setEmail(userData.getEmail());
        userInfo.setTelephone(userData.getTelephone());
        userInfo.setPosition(userData.getTitle());
        if (!StringUtils.isEmpty(userData.getLeaderId())) {
            userInfo.setDirectLeader(new ArrayList<>());
            userInfo.getDirectLeader().add(userData.getLeaderId());
        }
        userInfo.setDepartments(userData.getDepartments(), userData.getMainDepartmentId());
        return userInfo;
    }

}
